﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class TagProductList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _name { get; set; }
        public string _productNo { get; set; }
        public string _productId { get; set; }
        public string _status { get; set; }
        public string _productKind { get; set; }
        public string _userAccount{get;set;}
        public string _uid{get;set;}
        public ShopTagInfo tagInfo{get;set;}

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _name = Request["name"] == null ? "" : Request["name"];
            _productNo = Request["pno"] == null ? "" : Request["pno"];
            _productId = Request["pid"] == null ? "" : Request["pid"];
            _status = Request["status"] == null ? "" : Request["status"];
            _productKind = Request["productkind"] == null ? "" : Request["productkind"];
            _userAccount = UserKit.GetUserAccountFailLogin();
            

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            //初始化控件值
            tb_search_name.Text = _name;
            tb_search_productno.Text = _productNo;

            if (!IsPostBack)
            {
                string opt = Request["opt"];
                hid_uid.Value = Request["uid"];
            }
            _uid = hid_uid.Value;

            //绑定产品状态下拉框
            ParamKit.Bind("ProductStatus", ddl_status, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ParamKit.Bind("ProductKind", ddl_product_kind, null, new System.Collections.Generic.List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

            ddl_status.SelectedValue = _status;
            ddl_product_kind.SelectedValue = _productKind;

            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);

        }

        

        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                tagInfo=session.Get<ShopTagInfo>(int.Parse(_uid));

                ICriteria criteria = session.CreateCriteria<ShopProductInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            if (_name.Trim() != "")
            {
                criteria.Add(Expression.Like("ProductName", "%" + _name + "%"));
            }

            if (_productNo.Trim() != "")
            {
                criteria.Add(Expression.Like("ProductNo", "%" + _productNo + "%"));
            }

            if (_status.Trim() != "-1" && !string.IsNullOrEmpty(_status))
            {
                criteria.Add(Expression.Eq("Status", _status));
            }
            if (_productKind.Trim() != "-1" && !string.IsNullOrEmpty(_productKind))
            {
                criteria.Add(Expression.Eq("Kind", _productKind));
            }
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount)).AddOrder(Order.Asc("ProductNo"));
        }
        #endregion

        #region 查询数据总数 + protected int GetListCount()
        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopProductInfo>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
        #endregion

        

        protected long GetSkuProductCount(object no)
        {
            using (var session = SessionFactoryHelper.CreateSession())
            {
                var result = session.CreateQuery("select count(*) from ShopSkuProduct where ProductNo='" + no.ToString() + "'").UniqueResult();
                return result == null ? 0 : (long)result;
            }
        }

        protected string GetPicture(string path)
        {
            return path + ".thumb" + System.IO.Path.GetExtension(path);
        }

        protected string GetStatusHtml(string tids,string pno)
        {
            string statusHtml = "";
            bool isSeted = false;
            if(string.IsNullOrEmpty(tids)||tids.Length<3)
            {
                return "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + pno + "&tid=" + _uid + "&status=1'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认设置产品标签'   >设置产品标签</a>&nbsp;&nbsp;";
            }
            tids = tids.Substring(1,tids.Length-2);
            String[] tidArray = tids.Split('_');
           
            foreach(string tid in tidArray)
            {
                if (tid == _uid)
                {
                    isSeted = true;
                }
            }
            if (isSeted)
            {
                statusHtml += "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + pno + "&tid=" + _uid + "&status=0'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认删除产品标签'   >删除产品标签</a>&nbsp;&nbsp;";
            }
            else
            {
                statusHtml += "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + pno + "&tid=" + _uid + "&status=1'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认设置产品标签'   >设置产品标签</a>&nbsp;&nbsp;";
            }
        
            return statusHtml;
        }
    }
}
